﻿using blackhouse.Translator.Business;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Business.Translate;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blackhouse.WebNemo.Translator {
    public class WebNemoTranslatorResource : ITranslatorResource {

        #region Fields

        private IGroup nemoGroup;
        private blackhouse.WebNemo.Business.Translate.Translator translator;
        private CultureInfo defaultCulture;

        #endregion

        #region Properties

        public string Key {
            get { return this.nemoGroup.SystemName.ToLower(); }
        }

        public string Name {
            get { return this.nemoGroup.Name; }
        }

        #endregion

        #region Constructors

        public WebNemoTranslatorResource(IGroup group) {
            this.nemoGroup = group;
            this.defaultCulture = new TranslatorLanguagesManager().GetDefaultLanguage();
            this.translator = new blackhouse.WebNemo.Business.Translate.Translator(this.nemoGroup, this.defaultCulture);
        }

        #endregion

        #region Methods

        public string[] GetAllKeys() {
            int[] columns = this.nemoGroup.Columns.Where(c => !c.HideForEdition && c.ColumnType == FieldType.ValueField && c.DataType == typeof(string)).Select(c => c.ColumnId).ToArray();
            GroupRecords records = new GroupRecords(this.nemoGroup);
            var qKeys = from c in columns
                        from r in records
                        orderby c, r.RecordId
                        select TranslateKey.GetKey(this.nemoGroup.Id, c, r.RecordId);
            return qKeys.ToArray();
        }

        public string GetComment(CultureInfo language, string key) {
            TranslateKeyInfo keyInfo = TranslateKey.ParseKey(key);
            return new GroupRecord(keyInfo.GroupId, keyInfo.RecordId).Identyficator + Environment.NewLine + "Kolumna: " + this.nemoGroup.Columns[keyInfo.ColumnId].Name;
        }

        public IResourceEditor GetEditor(CultureInfo language) {
            return new WebNemoTranslatorEditor(language, this.translator);
        }

        public string GetText(CultureInfo language, string key) {
            TranslateKeyInfo keyInfo = TranslateKey.ParseKey(key);
            return this.translator.GetText(language, keyInfo.ColumnId, keyInfo.RecordId);
        }

        #endregion

    }
}
